/*
	checks given host for RPCID
	nice rpc functions useable for other various kiddie scanners!!

	ryan@junker.org 

*/

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>

#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>

#define TIMEOUT 15
#define RPCID 100024

char *nslookup(char *);
struct pmaplist *rpcinfo(char *);
int checkrpc (struct pmaplist *, int);
void sh(int);

int main(int argc, char **argv) {
	struct pmaplist *p;
	
	if (argc != 2) return 0;

	signal(SIGALRM, sh);
	alarm(TIMEOUT);

	p=rpcinfo(argv[1]);
	
	if (checkrpc(p, RPCID) == RPCID) 
		printf("%s\n", argv[1]);
	
	return 0;
}
struct pmaplist *rpcinfo(char *addr) {
  struct sockaddr_in s;
  struct pmaplist *ret;

  s.sin_family = AF_INET;
  s.sin_port = htons (111);
  s.sin_addr.s_addr = inet_addr(nslookup(addr));

  ret=pmap_getmaps(&s);
  return ret;
}

int checkrpc (struct pmaplist *pm, int rpcid) {
  if (pm == NULL) return 0;

  while (pm != NULL) {
      pm = pm->pml_next;

      if (pm->pml_map.pm_prog == rpcid) return rpcid;

      if (pm->pml_next == NULL)
        return 0;
  }
}

char *nslookup (char *addr) {
   struct hostent *h;

   if ((h=gethostbyname(addr)) == NULL) {
      perror("gethostbyname");
      exit(0);
   }

   return (char *)inet_ntoa(*((struct in_addr *)h->h_addr))        ;
}

void sh(int s) {
	exit(0);
}
